<?php
namespace Home\Controller;


/**
 * @Class       虚拟图层相关操作
 * @description 超图三维展示，实现图层的多层分类.
 * @Adres       [操作地址]/index.php/Home/Virtuallayer/listLayer.html  
 * @Author      fly
 */
class VirtuallayerController extends BaseController {

    //
    private $widget;

    /*
     * 各属性对应的名称
     */
    private $property = array(

    );


    /*
     * 模拟图层图层列表
     */
    public function listlayer(){
        $info = M('layer') -> order('id asc') -> select();
        $info = flytree($info);
        //page
        if(count($info)>10){
            $page = new \Think\Page(count($info), 10, I('get.'));
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $p = $page->show();
            $this->assign('_page' , $p ? $p : '');
            $this->assign('_total' , count($info));
            $info = array_slice($info , $page->firstRow , $page->listRows);
        }
        foreach($info as $v){
            $v['defaultshowtext'] = $v['defaultshow'] ? '显示' : '隐藏';
            if($v['upid']>0)
                $v['upname'] = M('layer') -> where(array('id'=>$v['upid'])) -> getField('layername');
            else
                $v['upname'] = "无";
            $result[] = $v;
        }
        $this -> assign('list' , $result);
        $this -> display();
    }


    /*
     * 添加模拟图层
     */
    public function addlayer(){
        //记录行为
        action_log('PC', 'Virtuallayer_addlayer', UID, UID);
        //
        if($_POST){
            $save = array(
                'layername' => I('post.name'),
                'upid' => I('post.upid') ? I('post.upid') : 0
            );
            //匹配
            if(!$save['layername'])  $this->ferror("虚拟图层名称不能为空");
            $findval = M('layer') -> where($save) -> find();
            if($findval)
                $this -> ferror('已经存在');

            //开始执行
            $save['ttime'] = time();
            $save['defaultshow'] = I('post.defaultshow')=='show' ? 1 : 0;
            M('layer') -> add($save);
            $this -> fsucces('ok');
        }
        if(I('get.id')){
            $info = M('layer') -> where(array('id'=>I('get.id'))) -> find();
            $info['defaultshow'] = $info["defaultshow"]==1 ?  'show' : 'hide';
        }else{
            $info = array(
                'id' => '0',
                'layername' => '无'
            );
        }
        $this -> assign('info' , $info);
        $this -> display();
    }


    /*
     * 模拟图层数据列表
     */
    public function listlayerInfo(){
        $layerid = I('get.id');
        $info = $this -> flylists('layerinfo',"`layerid`='$layerid' and `status`=1",'*','','layerid asc,id asc');
        $this -> assign('list' , $info);
        $this -> assign('layerid' , $layerid);
        $this -> display();
    }


    /*
     * 添加模拟图层的数据
     */
    public function addlayerinfo(){
        //记录行为
        action_log('PC', 'Virtuallayer_addlayerinfo', UID, UID);
        //
        if(I('post.')){
            $save = array(
                'mapname' => I('post.name'),
                'mapid' => I('post.mapid'),
                'layername' => I('post.layer'),
                'layerid' => I('post.layerid'),
                'status' => 1
            );
            //匹配
            if(!$save['mapname'])  $this->ferror("图层名称不能为空");
            if(!$save['layerid'])  $this->ferror("虚拟图层ID不能为空");
            $findval = M('layerinfo') -> where($save) -> find();
            if($findval)
                $this -> ferror('已经存在');

            //开始执行
            $save['ttime'] = time();
            if(I('post.id')){
                M('layerinfo') -> where(array('id'=>I('post.id'))) -> save($save);
            }else{
                M('layerinfo') -> add($save);
            }
            $this -> fsucces('ok');
        }
        if(I('get.id')){
            $list = M('layer') -> where(array('id'=>I('get.id'))) -> find();
            if(I('get.layerinfoid')){
                $layerinfo = M('layerinfo') -> where(array('id'=>I('get.layerinfoid'))) -> find();
                $list['mapname'] = $layerinfo["mapname"];
                $list['mapid'] = $layerinfo["mapid"];
                $this -> assign('layerinfoid' , I('get.layerinfoid'));
            }
            $this -> assign('list' , $list);
            $this -> assign('layerid' , I('get.id'));
        }


        $this -> display();
    }



    /*
     * 接口
     * 删除模拟图层
     */
    public function dellayerinfo(){
        //记录行为
        action_log('PC', 'Virtuallayer_dellayerinfo', UID, UID);
        //
        if(I('post.layer'))  $save['layername'] = I('post.layer');
        if(I('post.layerid'))  $save['id'] = I('post.layerid');
        if(!$save) $this -> ferror("参数错误");

        M('layerinfo') -> where($save) -> delete();
        $this -> fsucces('ok');
    }





    /*
     *
     */

}
